package luongvd.handyenglish.control;

import java.util.ArrayList;

import luongvd.handyenglish.R;
import luongvd.handyenglish.model.adapter.AdapterMenu;
import luongvd.handyenglish.model.object.ObjectItemMenu;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityHome extends FragmentActivity implements AnimationListener, OnClickListener{
	
	//definition for menu
	static class AnimParams {
        int left, right, top, bottom;

        void init(int left, int top, int right, int bottom) {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }
    }
	View menu;
    View app;
   
    ImageButton btnCallMenu;
    ListView listMenu;
    TextView titleApp;
    
    //animation for menu
    Animation anim;
    boolean menuOut;
    AnimParams animParams;
    int left;
    
    //
	ArrayList<ObjectItemMenu> arrayListMenu;
	ArrayAdapter<ObjectItemMenu> adapterMenu;
	ObjectItemMenu objectItemMenu;
	String[] arrayTitleMenu;
    
	public static final int position_dictionary = 0, position_grammar = 1,position_video = 2,position_vocabulary = 3,position_setting = 4,position_help = 5,position_about = 6;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_home);
		//call void get DB for listView menu
		getDB();
		//call void initwidget
		initWidGet();
	}

	
	//void get DB
	private void getDB() {
		arrayListMenu = new ArrayList<ObjectItemMenu>();
		arrayTitleMenu = getResources().getStringArray(R.array.arrar_item_menu);
		for(int i=0; i< arrayTitleMenu.length; i++)
		{
			objectItemMenu = new ObjectItemMenu();
			objectItemMenu.setIcon("");
			objectItemMenu.setTitle(arrayTitleMenu[i]);
			arrayListMenu.add(objectItemMenu);
		}
		adapterMenu = new AdapterMenu(this, arrayListMenu);
	}

	private void initWidGet() {
		
		menuOut = false;
		animParams = new AnimParams();
		
		menu = findViewById(R.id.ln_menu);
		listMenu = (ListView)menu.findViewById(R.id.list_menu);
		
		listMenu.setAdapter(adapterMenu);
		listMenu.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				//close menu
				showMenu();
				//
				setItemWhenClick(position);
			}
		});
		
        app = findViewById(R.id.ln_app);
        btnCallMenu = (ImageButton) app.findViewById(R.id.btnCallMenu);
        btnCallMenu.setOnClickListener(this);
        titleApp = (TextView)app.findViewById(R.id.titleApp);
	}

	void setItemWhenClick(int position) {
//		setTitleApp(position);
		Intent intent;
		switch (position) {
		case position_dictionary:
		{
			Fragment fragment = new FragmentDictionary();
			Bundle args = new Bundle();
			fragment.setArguments(args);

			FragmentManager fragmentManager = getSupportFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_fragment, fragment).commit();
		}
			break;
		case position_vocabulary:
		{
			Fragment fragment = new FragmentDictionary();
			Bundle args = new Bundle();
			fragment.setArguments(args);

			FragmentManager fragmentManager = getSupportFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_fragment, fragment).commit();
		}
			break;
		case position_grammar:
			intent = new Intent(this, ActivityDoingBuild.class);
			startActivity(intent);
			break;
		case position_video:
			intent = new Intent(this, ActivityDoingBuild.class);
			startActivity(intent);
			break;
		case position_setting:
			intent = new Intent(this, ActivitySetting.class);
			startActivity(intent);
			break;
		case position_help:
			intent = new Intent(this, ActivityHelp.class);
			startActivity(intent);
			break;
		case position_about:
			intent = new Intent(this, ActivityAbout.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	// void set title
	void setTitleApp(int position){
		if(position < 4){
			titleApp.setText(arrayTitleMenu[position]);
		}
	}
	
	void layoutApp(boolean menuOut) {
      app.layout(animParams.left, animParams.top, animParams.right, animParams.bottom);
      app.clearAnimation();
  }
	
	@Override
	public void onAnimationEnd(Animation arg0) {
        if (menuOut == false) {
            menu.setVisibility(View.INVISIBLE);
        }
        layoutApp(menuOut);
	}

	@Override
	public void onAnimationRepeat(Animation arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationStart(Animation arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnCallMenu:
		{
			showMenu();
		}
			break;

		default:
			break;
		}
		
	}
	
	void closeMenu(){
        menuOut = false;
	}

	//void show menu
	void showMenu(){
	
		int w = app.getMeasuredWidth();
        int h = app.getMeasuredHeight();
        left = (int) (app.getMeasuredWidth() * 0.8);
        if (menuOut == false) {
			menuOut = true;
            anim = new TranslateAnimation(0, left, 0, 0);
            menu.setVisibility(View.VISIBLE);
            animParams.init(left, 0, left + w, h);
        } else {
        	menuOut = false;
            anim = new TranslateAnimation(0, -left, 0, 0);
            animParams.init(0, 0, w, h);
        }
        anim.setDuration(300);//time to show menu 300 mini s
        anim.setAnimationListener(this);
        anim.setFillAfter(true);
        app.startAnimation(anim);
	}
/*	
	//event when touch on screen
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x1 = 0,x2 = 0;
		float y1 = 0,y2 = 0;
		int eventaction = event.getAction();
		switch (eventaction) {
        case MotionEvent.ACTION_DOWN: 
        	
        	x1 = event.getX();
        	y1 = event.getY();
        	Log.d("Luong", "****X1: "+x1);
        	Log.d("Luong", "****Y1: "+y1);
             break;

        case MotionEvent.ACTION_MOVE:
            break;

        case MotionEvent.ACTION_UP:   
        	x2 = event.getX();
        	y2 = event.getY();
        	Log.d("Luong", "____________X2: "+x2);
        	Log.d("Luong", "____________Y2: "+y2);
        	if ((x2 - x1) > 50 && Math.abs(x2 - x1) > Math.abs(y2 - y1)) 
            {
        		Log.d("Luong", "------- left to right");
//        		showMenu(Const.TOUCH_LEFT_TO_RIGHT);
            }else if ((x1 - x2) > 50 && Math.abs(x2 - x1) > Math.abs(y2 - y1)) {
            	Log.d("Luong", "+++++++ right to left");
//            	showMenu(Const.TOUCH_RIGHT_TO_LEFT);
			}
            break;
		}

		
		return false;
	}

*/
}
